﻿using System.Data.Entity;
using DataAccess.Models;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Data.Entity.Infrastructure;

namespace DataAccess
{
	public class KPIDbContext : DbContext
	{
		public DbSet<User> User
		{
			get;
			set;
		}

		public DbSet<Group> Group
		{
			get;
			set;
		}

		public DbSet<GroupCategory> GroupCategory
		{
			get;
			set;
		}

		public DbSet<KPI> KPI
		{
			get;
			set;
		}

		public DbSet<KPIForUpdate> KPIForUpdate
		{
			get;
			set;
		}

		public DbSet<KPIProgress> KPIProgress
		{
			get;
			set;
		}

		public DbSet<KPIProgressForUpdate> KPIProgressForUpdate
		{
			get;
			set;
		}

		public DbSet<PolicyDocuments> PolicyDocuments
		{
			get;
			set;
		}

		public DbSet<WorkNews> WorkNews
		{
			get;
			set;
		}

		public DbSet<MailMessage> MailMessages
		{
			get;
			set;
		}

		public DbSet<MailReciever> MailRecievers
		{
			get;
			set;
		}

		protected override void OnModelCreating(DbModelBuilder modelBuilder)
		{
			//Database.SetInitializer<KPIDbContext>(null);

			modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

			modelBuilder.Entity<Group>().HasRequired(g => g.TheGroupCategory).WithMany(gc => gc.ListGroups).HasForeignKey(g => g.GroupCategoryID);
			modelBuilder.Entity<KPIForUpdate>().HasRequired(kpiu => kpiu.TheKPI).WithRequiredDependent(kpi => kpi.TheKPIForUpdate);
			modelBuilder.Entity<KPIProgress>().HasRequired(kpip => kpip.TheKPI).WithRequiredDependent(kpi => kpi.TheKPIProgress);
			modelBuilder.Entity<KPIProgressForUpdate>().HasRequired(kpipu => kpipu.TheKPI).WithRequiredDependent(kpi => kpi.TheKPIProgressForUpdate);
			modelBuilder.Entity<KPI>().HasRequired(kpi => kpi.TheGroup).WithMany(g => g.ListKPIs);
			modelBuilder.Entity<MailReciever>().HasKey(mr => new
			{
				mr.MailID,
				mr.Reciever
			});
			modelBuilder.Entity<MailMessage>().HasMany(m => m.ListMailReciever).WithRequired(mr => mr.TheMailMessage);
		}
	}
}
